// pages/releaseinfo/release_home/release_home.js
var utils = require('../../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var codeName = getApp().globalData.codeName;
var userId = getApp().globalData.userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filePaths: [],  //上传图片的列表
    name: "",
    checked1: false,  //我要置顶
    checked2: false, //我要精选
    total: 0.0,
    objectArray: ["1", "2", "3", "4", "5", "6", "7"],
    zhidingjiaNum: "", //我要置顶价格
    jingxuanjiaNum: "" //我要精选价格
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      locationAddress: wx.getStorageSync("city").cityname
    })
    utils.http(httpUrl + "getPrice", { code: codeName }, function (res) {
      console.log(res);
      that.setData({
        zhidingjia: res.zhidingjia.price,  //置顶
        jingxuanjia: res.jingxuanjia.price, //精选
        shangjia: res.shangjia,   //商家
        num: res.fabu.price,
        total: res.fabu.price,
      })
      that.setData({
        zhidingjiaNum: that.data.zhidingjia,
        jingxuanjiaNum: that.data.jingxuanjia
      })
    })
  },
  //提交发布
  formSubmit: function (e) {
    var that = this;
    var title = e.detail.value.nameTitle;
    var area1 = e.detail.value.area1;
    var house = e.detail.value.house;
    var price = e.detail.value.price;
    var description = e.detail.value.description;
    var location = that.data.locationAddress;
    var telephone = e.detail.value.telephone;
    var area1 = e.detail.value.area1;
    var carNumber = e.detail.value.carNumber;
    var top = "";  //是否置顶
    var choiceness = "" //是否为精选
    // console.log(that.data.listku);
    var imagelist = "";
    if (that.data.listku != undefined) {
      imagelist = that.data.listku.join(",");
    } else {
      imagelist = ""
    }
    //  console.log(imagelist);
    if (that.data.checked1) {
      //   console.log("我要置顶" + that.data.checked1);
      top = 1;
    } else {
      top = 0;
      //  console.log("我不要置顶" + that.data.checked1);
    }
    if (that.data.checked2) {
      //   console.log("checked2我要精选" + that.data.checked2);
      choiceness = 1;
    } else {
      choiceness = 0;
      //  console.log("checked2我不要要精选" + that.data.checked2);
    }
    console.log(that.data.choicenessDay);
    // if (choiceness==1){
    //   if (that.data.choicenessDay == undefined){
    //     wx.showToast({
    //       title: '请输选择天数',
    //       image: "/images/errer.png",
    //       duration: 2000
    //     })
    //   }
    // }
    if (title != "" && area1 != "" && house != "" && price != "", description != "" && imagelist != "" && telephone != "") {
      utils.http(httpUrl + "publishHouseSale", {
        type: 1,
        orderType: 1,
        code: codeName,
        userid: userId,
        title: title,//标题
        area1: area1,//	面积
        house: house,//户型
        price: price,//	价格
        description: description,//描述
        location: location, //	地址
        latitude: that.data.latitude,//latitude    精度
        longitude: that.data.longitude,//longitude  纬度
        telephone: telephone,//	电话
       // top: top, //	是否置顶 0为否 1为是
        stickDay: that.data.topDay,//置顶几天
      //  choiceness: choiceness,//是否精选 0为否 1为是
        choiceDay: that.data.choicenessDay,//精选几天
        totalPrice: that.data.total.toFixed(1),  //费用
        images: imagelist

      }, function (res) {
        console.log(res.order);
        utils.http(httpUrl + "preWechat", {
          ordernum: res.order.orderNo
        }, function (res) {
          console.log(res.wechat);
          var wechat = JSON.parse(res.wechat);
          wx.requestPayment({
            'appId': wechat.appID,
            'timeStamp': wechat.timeStamp,
            'nonceStr': wechat.nonceStr,
            'package': wechat.package,
            'signType': wechat.signType,
            'paySign': wechat.paySign,
            'success': function (res) {
            },
            'fail': function (res) {
            }
          })
        })
      })
    } else {

      if (telephone == "") {
        wx.showToast({
          title: '请输入手机号',
          image: "/images/errer.png",
          duration: 2000
        })
      }
      if (imagelist == "") {
        wx.showToast({
          title: '请上传图片',
          image: "/images/errer.png",
          duration: 2000
        })
      }
      if (description == "") {
        wx.showToast({
          title: '请输入描述',
          image: "/images/errer.png",
          duration: 2000
        })
      }
      if (price == "") {
        wx.showToast({
          title: '请输入价格',
          image: "/images/errer.png",
          duration: 2000
        })
      }
      if (house == "") {
        wx.showToast({
          title: '请输入户型',
          image: "/images/errer.png",
          duration: 2000
        })
      }
      if (area1 == "") {
        wx.showToast({
          title: '请输入面积',
          image: "/images/errer.png",
          duration: 2000
        })
      }
      if (title == "") {
        wx.showToast({
          title: '请输入名称',
          image: "/images/errer.png",
          duration: 2000
        })
      }






    }

  },
  // 图片上传
  oNuploadTap: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          tempFilePaths: tempFilePaths
        })
        wx.uploadFile({
          url: httpUrl + 'uploadImg',
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (e) {
            that.setData({
              images: JSON.parse(e.data).imgs
            })
            var listku = that.data.listku;
            if (listku == undefined) {
              listku = []
            }
            listku.push(that.data.images);
            that.setData({
              listku: listku
            })
            console.log(listku);
          }
        })

      }
    })


  },

  //获取地图导航
  oNmapTap: function (e) {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        //    console.log(res)
        that.setData({
          locationAddress: res.address,
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })

  },
  //我要置顶
  switchChange1: function (e) {
    var checked = e.detail.value;
    if (checked) {
      console.log(checked + "我要置顶")
     // console.log(this.data.objectArray[0]);
      for (var i in this.data.objectArray) {
        this.setData({
          object1: this.data.objectArray[0],
          zhidingjiaNum: this.data.zhidingjia,
          topDay:this.data.objectArray[0]
        })
      }
      //  console.log(this.data.checked2+"精选");
      if (this.data.checked2 == false) {
        this.setData({
          jingxuanjiaNum: 0,
          total: this.data.num
        })
      }
      this.getTotalPrice();
    } else {
      console.log(checked + "我不要要置顶")
      this.setData({
        zhidingjiaNum: 0,
        index: -1,
        topDay:0,
        total: this.data.num
      })
      this.getTotalPrice();
      // console.log(this.data.zhidingjia);
      //console.log(this.data.index + "value值");
    }
    this.setData({
      checked1: checked
    })
  },
  //选择置顶天数下拉框
  bindPickerChange1: function (e) {
    //    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value;
    this.setData({
      index: index
    })
    //console.log(this.data.zhidingjia + "原有的价格")
    //console.log(this.data.objectArray[index] + "天数")
    var numPrice = this.data.zhidingjia * this.data.objectArray[index];   //置顶天数
    // console.log(numPrice);
    this.setData({
      zhidingjiaNum: numPrice.toFixed(1),
      topDay: this.data.objectArray[index],
      total: this.data.num
    })
    this.getTotalPrice();
  },
  // //我要精选
  switchChange2: function (e) {
    var checked = e.detail.value;
    if (checked) {
      for (var i in this.data.objectArray) {
        this.setData({
          object2: this.data.objectArray[0],
          jingxuanjiaNum: this.data.jingxuanjia,
          choicenessDay: this.data.objectArray[0]
        })
      }
      //console.log(this.data.checked1  + "我要置顶")
      if (this.data.checked1 == false) {
        this.setData({
          zhidingjiaNum: 0,
          total: this.data.num
        })
      }
      this.getTotalPrice();
    } else {
      // console.log(checked + "我不要要置顶")
      this.setData({
        jingxuanjiaNum: 0,
        index1: -1,
        choicenessDay:0,
        total: this.data.num
      })
      this.getTotalPrice();
      //console.log(this.data.jingxuanjia);
      // console.log(this.data.index1 + "value值");
    }
    this.setData({
      checked2: checked
    })

  },
  //选择精选天数下拉框
  bindPickerChange2: function (e) {
    //  console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value;
    this.setData({
      index1: index
    })
    // console.log(this.data.jingxuanjia + "原有的价格")
    // console.log(this.data.objectArray[index] + "天数")
    var numPrice = this.data.jingxuanjia * this.data.objectArray[index];   //置顶天数
    // console.log(numPrice);
    this.setData({
      jingxuanjiaNum: numPrice.toFixed(1),
      choicenessDay:this.data.objectArray[index],
      total: this.data.num
    })
    // console.log(this.data.jingxuanjiaNum + "总价格")
    this.getTotalPrice();
  },

  //费用==时间* 价格
  getTotalPrice: function (e) {
    var p = parseFloat(this.data.jingxuanjiaNum) + parseFloat(this.data.zhidingjiaNum) + parseFloat(this.data.num)
    // console.log(p);
    this.setData({
      total: p
    })
  }
})